const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  TheClass,
  Student,
} = require('../db/model');

class StudentService extends Service {
  async getAllStudent() {
    await TheClass.hasMany(Student);
    await Student.belongsTo(TheClass, {
      foreignKey: 'tcId',
    });
    const res = await Student.findAll({
      attributes: { exclude: ['TheClassTcId'] },
      include: TheClass,
    }).catch(err => {
      console.log(`Student 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`Student 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getStudent(student) {
    if (!student?.sId) {
      console.log(`Student 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${student}`, student);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    await TheClass.hasMany(Student);
    await Student.belongsTo(TheClass, {
      foreignKey: 'tcId',
    });
    const res = await Student.findOne({
      where: {
        sId: student?.sId || -1
      },
      attributes: { exclude: ['TheClassTcId'] },
      include: TheClass,
    }).catch(err => {
      console.log(`Student 表查找失败: ${err}\n参数: ${student}`, student);
    });
    if (res === undefined) {
      console.log(`Student 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${student}`, student);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`Student 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${student}`, student);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendStudent(student) {
    let res = await this.getStudent(student);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`Student 表新增失败: ${res}\n参数: ${student}`, student);
      return res;
    }
    res = await Student.create(student).catch(err => {
      console.log(`Student 表新增失败: ${err}\n参数: ${student}`, student);
    });
    if (res === undefined) {
      console.log(`Student 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${student}`, student);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateStudent(student) {
    let res = await this.getStudent(student);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Student 表更新失败: ${res}\n参数: ${student}`, student);
      return res;
    }
    res = await Student.update(student, {
      where: {
        sId: student?.sId || -1
      },
    }).catch(err => {
      console.log(`Student 表更新失败: ${err}\n参数: ${student}`, student);
    });
    if (res === undefined) {
      console.log(`Student 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${student}`, student);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyStudent(student) {
    let res = await this.getStudent(student);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Student 表删除失败: ${res}\n参数: ${student}`, student);
      return res;
    }
    res = await Student.destroy({
      where: {
        sId: student?.sId || -1
      },
    }).catch(err => {
      console.log(`Student 表删除失败: ${err}\n参数: ${student}`, student);
    });
    if (res === undefined) {
      console.log(`Student 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${student}`, student);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = StudentService;
